
@* Generator: Template*@
@using System.Collections
@using System.Reflection

@functions {
    public Type Type{ get; set; }
}



@{
    var props= Type.GetProperties();
}

<form role="form">
    @foreach (var propertyInfo in props)
    {
        <div class="form-group">
            <label for="@(propertyInfo.Name)lbl">@propertyInfo.Name</label>
            <input type="text" class="form-control" id="@(propertyInfo.Name)lbl" placeholder="Enter @propertyInfo.Name">
        </div>
    }
</form>

    @functions
{

        public string Generate(PropertyInfo[] propertyInfos)
        {
            var ps = propertyInfos.Select(c => "a." + c.Name);
            return string.Join(",", ps);
        }

    }
    <script>
        function @(Type.Name)Model(){
            var self = this;

            @{
    var properties = Type.GetProperties();
}

            @foreach (var p in properties)
{
    if (p.PropertyType == typeof (string))
    {
        <text>
            self.@(p.Name) = ko.observable(item.@(p.Name));
            </text>
    }
    else
    {
        if (typeof(IEnumerable).IsAssignableFrom(p.PropertyType))
        {
            var arguemnts = p.PropertyType.GetGenericArguments();

            if (arguemnts.Length > 0)
            {
                Type myListElementType = arguemnts[0];

                <text>
            var temp@(myListElementType.Name)s = $.isEmptyObject(item.@(p.Name)) ? [] : $.map(item.@(p.Name), function(a) {
                return new @(myListElementType.Name)Model(@(Generate(myListElementType.GetProperties())));
            });
            </text>
            }
        }
        else
        {
            <text>
            self.@(p.Name) = ko.observable(item.@(p.Name));
            </text>
        }
    }
}
        }

    </script>
